<template>
  <div>
    <el-dialog :title="info.isAdd?'规格添加':'规格编辑'" :visible.sync="info.isshow" @closed="closed">
      <el-form :model="form">
        {{form}}
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="规格属性" label-width="120px" v-for="(item,index) in arr" :key="index">
          <div class="box">
            <el-input v-model="item.value" autocomplete="off"></el-input>
            <el-button type="primary" v-if="index==0" @click="addArr">新增规格属性</el-button>
            <el-button type="danger" v-else @click="delArr(index)">删除</el-button>
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { reqspecsadd, reqspecsedit, reqspecsinfo } from "../../../request/api";
import { successAlert ,errorAlert} from "../../../utils/alert";
import { mapActions } from "vuex";
export default {
  components: {},
  props: ["info"],
  data() {
    return {
      form: {
        specsname: "",
        attrs: "",
        status: 2,
      },
      arr: [
        {
          value: "",
        },
      ],
    };
  },

  mounted() {},
  methods: {
    ...mapActions({
      reqlist: "specs/reqlist",
      //请总数
      reqcount: "specs/reqcount",
    }),

    //新增规格属性
    addArr() {
      this.arr.push({
        value: "",
      });
    },
    //删除规格属性
    delArr(index) {
      this.arr.splice(index, 1);
    },
    //取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    empty() {
      this.form = {
        specsname: "",
        attrs: "",
        status: 2,
      };
      this.arr = [
        {
          value: "",
        },
      ];
    },
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.specsname == "") {
          errorAlert("规格名称不能为空");
          return;
        }
        this.arr.forEach((item) => {
          if (item.value == "") {
            errorAlert("规格属性不能为空");
            return;
          }
        });
        resolve();
      });
    },
    add() {
        // console.log(this.arr.map((item) => item.value));
        // console.log(this.arr.map((item) => item.value).join(","));
        this.form.attrs = this.arr.map((item) => item.value).join(",");
        reqspecsadd(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);
            this.cancel();
            this.empty();
            this.reqlist();
            this.reqcount();
          }
        });
    },
    getOne(id) {
      reqspecsinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list[0];

          this.arr = this.form.attrs.map((item) => ({ value: item }));
        }
      });
    },

    update() {
      this.form.attrs = this.arr.map((item) => item.value).join(",");
      reqspecsedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);

          this.cancel();

          this.empty();

          this.reqlist();
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.box {
  display: flex;
}
.box .el-input {
  flex: 1;
}
.box .el-button {
  width: auto;
}
</style>